package NetModel.okhttp;

import android.util.Log;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import okhttp3.Call;
import okhttp3.CipherSuite;
import okhttp3.ConnectionSpec;
import okhttp3.OkHttpClient;
import okhttp3.TlsVersion;

/**
 * Created by Administrator on 2016/10/26.
 * 设置Okhttp的基础参数设置和任务取消类
 */
public class OkHttpClientManger {

    private static final String TAG = "OkHttpClientManger";
    public static final int DEFAULT_CONNECT_TIMEOUT = 10000;
    public static final int DEFAULT_READ_TIMEOUT = 8000;
    public static final int DEFAULT_WRITE_TIMEOUT = 8000;

    private static OkHttpClientManger instance;
    private static volatile OkHttpClient client;


    private OkHttpClientManger(OkHttpClient client) {
        this.client = client;
    }


    public static OkHttpClientManger getInstance() {
        if (instance == null) {
            synchronized (OkHttpClientManger.class) {
                if (instance == null)
                    instance = new OkHttpClientManger(getDefaultClient());
            }
        }
        return instance;
    }


    private static OkHttpClient getDefaultClient() {
        ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
                .tlsVersions(TlsVersion.TLS_1_2)
                .cipherSuites(
                        CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
                        CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
                        CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256)
                .build();
        OkHttpClient defaultClient = new OkHttpClient.Builder()
                .connectionSpecs(Collections.singletonList(spec))
                .connectTimeout(DEFAULT_CONNECT_TIMEOUT, TimeUnit.MILLISECONDS)
                .readTimeout(DEFAULT_READ_TIMEOUT, TimeUnit.MILLISECONDS)
                .writeTimeout(DEFAULT_WRITE_TIMEOUT, TimeUnit.MICROSECONDS)
                .hostnameVerifier(new MyHostnameVerifier())
                .build();
        return defaultClient;


    }


    public static class MyHostnameVerifier implements HostnameVerifier {
        @Override
        public boolean verify(String hostname, SSLSession session) {
            Log.i(TAG, hostname);
            return true;
        }
    }


    public void setClient(OkHttpClient client) {
        this.client = null;
        this.client = client;
    }

    public OkHttpClient getClient() {
        return client;
    }


    public void cancel(Object tag) {
        List<Call> waitingCall = getClient().dispatcher().queuedCalls();
        for (Call call : waitingCall) {
            if (tag.equals(call.request().tag())) {
                call.cancel();
            }
        }

        List<Call> runningCall = getClient().dispatcher().runningCalls();
        for (Call call : runningCall) {
            if (tag.equals(call.request().tag())) {
                call.cancel();
            }
        }
    }


}
